package org.mm.service.impl;

import java.util.List;

import org.mm.service.RoleService;
import org.mm.model.Role;
import org.mm.repository.RoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

/**
 * RoleServiceImpl
 *
 * @author Marco Traversari
 * @version $Revision: $ $Date: $ $Author: $
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
	
	@Autowired
	private RoleRepository roleRepository;
	
	@Transactional(readOnly = true)
	public List<Role> findAll() throws Exception {
		return Lists.newArrayList(roleRepository.findAll(new Sort(Sort.Direction.ASC, "id")));
	}
	
	@Transactional(readOnly = true)
	public Role findById(Long id) throws Exception {
		return roleRepository.findOne(id);
	}

	@Transactional
	public Role save(Role role) throws Exception {
		return roleRepository.save(role);
	}

	@Transactional(readOnly = true)
	public Page<Role> findAllByPage(Pageable pageable) throws Exception {
		return roleRepository.findAll(pageable);
	}

	@Transactional
	public void delete(Role role) throws Exception {
		roleRepository.delete(role);
	}
	
	@Transactional(readOnly = true)
	public Page<Role> search(Role role) throws Exception {
		Page<Role> page = null;
		/*		if (role.getId() != null) {
			PageRequest pageRequest 
			page = roleRepository.findOne(pageRequest);
		}*/
		return page;
		
	}
	
}
